<?php
namespace app\oem\controller;
use think\Controller;
use think\request;
use think\Db;


class Login extends controller
{
    public function index()
    {
    	//  //判断是否是https
        // if(empty(is_https())){
        // 	$url =  'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        // 	header("Location: ".$url);  
        // }
		if (session('d_id')) {
            $this->redirect('oem/index/index');
        }
        return  $this->fetch();
    }
    
    
    public function dologin(){
    	 if(request()->isPost()){ 
    	 	$data['mobile']= $_POST['oem_mobile'];
    	 	$data['password']=md5_salt($_POST['oem_password']);
			$kf['kfmobile']= $_POST['oem_mobile'];
    	 	$kf['kfpassword']=md5_salt($_POST['oem_password']);
    	 	$user=Db::table('info_oem')->where($data)->find();
			$res=Db::table('info_oem')->where($kf)->find(); 
    	 	if($user){
				if($user['status'] == 1){
	            	$status='登录成功';
					session('type', 1);
	            	session('d_id', $user['id']);
					session('mobile', $user['mobile']);
               		session('project_name', $user['project_name']);
               		session('oem_id', $user['oem_id']);
               		session('agency_id', $user['agency_id']);
               		session('user_type', $user['user_type']);
               		session('user_level', $user['user_level']);
               		return json(['code'=>200,'msg'=>$status]);
				}else{
	            	$status='<span style="color:red">帐号有问题</span>';
	            	return json(['code'=>402,'msg'=>$status]);
				}
    	 	}else if($res){
    	 		if($res['status'] == 1){
	            	$status='登录成功'; 
	            	session('d_id', $res['id']);
					session('mobile', $res['kfmobile']);
               		session('project_name', $res['project_name']);
               		session('oem_id', $res['oem_id']);
               		session('agency_id', $res['agency_id']);
               		session('user_type', $res['user_type']);
               		session('user_level', $res['user_level']);
               		return json(['code'=>201,'msg'=>$status]);
				}else{
	            	$status='<span style="color:red">帐号有问题</span>';
	            	return json(['code'=>402,'msg'=>$status]);
				}
    	 	}else{
            	$status='<span style="color:red">账户名或密码错误</span>';
            	return json(['code'=>408,'msg'=>$status]);
			}
    	 	
    	 	
    	 }
    	
    	
   	}
  	
  	/**
     * 修改密码
     */
    public function return_pwd() {
    	if(request()->isPost()){
    		
    		$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');

			if($result != 1) {
				$this->error('验证码错误或过期');
			}
    		$user_info = db('info_oem')->where('mobile',$_POST['mobile'])->find();
	    	if($user_info){
	    			$pwd = md5_salt($_POST['pwd']);
	    			$result = Db::table('info_oem')->where('id', $user_info['id'])->update(['password' => $pwd]);
	    			if($result){
	    				$this->success('修改成功','oem/index/login');
	    			}else{
	    				$this->error('修改失败');
	    			}
	   		}else{
	   			$this->error('用户不存在');
	   		}
    	}
    	return  $this->fetch();
    } 
  	/**
     * 退出登录
     */
    public function logout() {
        session(null);
        $this->success('退出成功', 'oem/login/index');
    } 
}
